package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItemGroup;
import com.itheima.service.CheckGroupService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.CheckItemGroupService;
import com.itheima.vo.CheckGroupVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper,CheckGroup> implements CheckGroupService {


    @Autowired
    private CheckItemGroupService checkItemGroupService;


    /**
     * 查询所有数据
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {

        //查询条件  是否删除
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);

        return this.list(queryWrapper);

    }

    //通过setmealId查找检查组信息
    @Override
    public List<CheckGroupVo> findBySetmealId(Long setmealId) {

        return baseMapper.findBySetmealId(setmealId);
    }

    //添加 修改
    @Override
    @Transactional
    public void addOrUpdate(CheckGroupDto checkGroup) {
        //保存检查组
        this.saveOrUpdate(checkGroup.getCheckGroup());

        //增加表关系之前 删除关系表数据
        checkItemGroupService.deleteByGroupId(checkGroup.getCheckGroup().getId());

        //维护关系数据
        List<CheckItemGroup> items = new ArrayList<>();
        for (Long itemId : checkGroup.getItemIds()){
            CheckItemGroup checkItemGroup = new CheckItemGroup();
            checkItemGroup.setCheckGroupId(checkGroup.getCheckGroup().getId());
            checkItemGroup.setCheckItemId(itemId);
            items.add(checkItemGroup);
        }

        //使用list链表  批量插入数据
        checkItemGroupService.saveBatch(items);

    }

    //分页显示内容
    @Override
    public PageResult findByPage(QueryPageBean pageBean) {
        //pageParam 分页参数
        Page<CheckGroup> checkGroupPage =
                new Page<CheckGroup>(pageBean.getCurrentPage(), pageBean.getPageSize());

        //过滤已删除
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag","0");


        //判断是否是条件搜索  如果是，继续拼加条件
        if (pageBean.getQueryString()!=null && "".equals(pageBean.getQueryString().trim())==false){
            //where delete_flag = 0
            //and(code like '' or name like '' ...)
            queryWrapper.and(checkGroupQueryWrapper ->
                    checkGroupQueryWrapper.like("code",pageBean.getQueryString())
                    .or()
                    .like("helpcode",pageBean.getQueryString())
                    .or()
                    .like("name",pageBean.getQueryString()));
        }


        //分页查询
        Page<CheckGroup> groupPage = this.page(checkGroupPage,queryWrapper);
        PageResult pageResult = new PageResult(groupPage.getTotal(), groupPage.getRecords());

        return pageResult;
    }

    /**
     *逻辑删除
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        //方法1
/*        //先查询
        CheckGroup checkGroup = baseMapper.selectById(id);
        //如果存在 修改deleteFlag字段
        if (checkGroup!=null){
            checkGroup.setDeleteFlag(1);
        }
        return this.updateById(checkGroup);*/

        //方法2  优化
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setDeleteFlag(1);
        return this.updateById(checkGroup);

    }


}
